package com.brillilab.service.core.method.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.method.MethodSearchMapper;
import com.brillilab.domain.enums.method.MethodSearchTypeEnum;
import com.brillilab.domain.po.method.MethodSearch;
import com.brillilab.domain.vo.method.MethodSearchVo;
import com.brillilab.service.core.method.IMethodSearchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 方法搜索
 * @author WuMenghao
 * @date    2019-03-01
 */
@Service
@Transactional
public class MethodSearchServiceImpl implements IMethodSearchService {

    @Resource
    private MethodSearchMapper methodSearchMapper;

    /**
     * 新增搜索记录
     * @param searchContent
     * @param searchType
     * @param userId
     * @return
     */
    @Override
    public boolean insertMethodSearchRecord(String searchContent,Integer searchType,Long userId) {
        Assert.isTrue(StringUtils.isNotBlank(searchContent.trim()) && searchType!=null && userId!=null,"参数缺失！");
        if(MethodSearchTypeEnum.METHOD_REFER.getType().equals(searchType)){
            MethodSearch methodSearch = new MethodSearch();
            methodSearch.setContent(searchContent);
            methodSearch.setType(searchType);
            methodSearch.setUserId(userId);
            return methodSearchMapper.insert(methodSearch) >0;
        }else if(MethodSearchTypeEnum.METHOD_REPOSITORY.getType().equals(searchType)){
            MethodSearch methodSearch=methodSearchMapper.selectOne(
                    new LambdaQueryWrapper<MethodSearch>()
                            .eq(MethodSearch::getContent,searchContent)
                            .eq(MethodSearch::getType,searchType)
                            .eq(MethodSearch::getUserId,userId));

            if(methodSearch!=null){
                methodSearch.setUpdateTime(new Date());
                return methodSearchMapper.updateById(methodSearch) >0;
            }else {
                methodSearch = new MethodSearch();
                methodSearch.setContent(searchContent);
                methodSearch.setType(searchType);
                methodSearch.setUserId(userId);
                return methodSearchMapper.insert(methodSearch) >0;
            }
        }else {
            return false;
        }
    }

    @Override
    public List<MethodSearchVo> getMethodReferenceHotSearchRecord() {
        return methodSearchMapper.selectMethodReferenceHotSearchRecordList();
    }

    @Override
    public List<MethodSearchVo> getMethodReferenceSearchHistory(Long userId) {
        Assert.notNull(userId,"参数缺失！");
        return methodSearchMapper.selectMethodReferenceSearchHistoryList(userId);
    }

    @Override
    public List<MethodSearchVo> getMethodRepositorySearchHistory(Long userId) {
        Assert.notNull(userId,"参数缺失！");
        return methodSearchMapper.selectMethodRepositorySearchHistoryList(userId);
    }

    @Override
    public boolean clearMethodSearchHistory(Long userId,Integer type) {
        Assert.isTrue(userId!=null && type!=null,"参数缺失！");
        return methodSearchMapper.delete(
                new LambdaQueryWrapper<MethodSearch>()
                        .eq(MethodSearch::getType,type)
                        .eq(MethodSearch::getUserId,userId)) > 0;
    }
}
